package org.smart.access.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.smart.access.dto.UserInfo;
import org.smart.access.mapper.HouseMapper;
import org.smart.access.model.House;
import org.smart.access.model.User;
import org.smart.access.service.HouseService;
import org.smart.access.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {

    @Lazy
    @Autowired
    private UserService userService;

    @Override
    public UserInfo getOwner(String building, String room) {
        House house = getOne(new LambdaQueryWrapper<House>()
                .eq(House::getBuilding, building)
                .eq(House::getRoom, room)
        );
        if (house == null) return null;
        List<User> userList = userService.list(new LambdaQueryWrapper<User>()
                .eq(User::getHouseId, house.getId())
        );
        if (userList.isEmpty()) return null;
        return UserInfo.of(userList.get(0), house, userList.get(0).getRealname());
    }
}
